package br.eng.eliseu.gwt.aconchego.camera.client.ui;

import br.eng.eliseu.gwt.aconchego.camera.client.service.UsuarioService;
import br.eng.eliseu.gwt.aconchego.camera.client.service.UsuarioServiceAsync;
import br.eng.eliseu.gwt.aconchego.camera.client.ui.component.EditAsyncCallback;
import br.eng.eliseu.gwt.aconchego.camera.module.AcessoCam;
import br.eng.eliseu.gwt.aconchego.camera.module.Usuario;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AlteraSenha extends Composite {

	interface AlteraSenhaUiBinder extends UiBinder<Widget, AlteraSenha> {}
	private static AlteraSenhaUiBinder uiBinder = GWT.create(AlteraSenhaUiBinder.class);
	
	private final UsuarioServiceAsync usuarioService = GWT.create(UsuarioService.class);
	private EditAsyncCallback<AcessoCam> callback;

	@UiField VerticalPanel alteraSenhaPanel;
	@UiField PasswordTextBox senhaAtualTBox;
	@UiField PasswordTextBox novaSenhaTBox;
	@UiField PasswordTextBox confirmaSenhaTBox;
	@UiField Label msgErroAlteraLabel;
	@UiField Button confirmaAlteracaoButton;
	@UiField Button cancelarAlteracaoButton;

	private AcessoCam usuarioLogin;

	public AlteraSenha(AcessoCam usuarioLogin) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.usuarioLogin = usuarioLogin;
	}

	@UiHandler("confirmaAlteracaoButton")
	void onConfirmaAlteracaoButtonClick(ClickEvent event) {
		if (checaCampoEmBrancoAltera()){
			if (!usuarioLogin.getUsuario().getSenha().equals(senhaAtualTBox.getValue())){
				showErroAltera("Senha atual nao confere");
				return;
			}
			trocaSenhaAsync();
		}

	}

	@UiHandler("cancelarAlteracaoButton")
	void onCancelarAlteracaoButtonClick(ClickEvent event) {
		if (getCallback()!=null){
			getCallback().onCancel();
		}
	}

	private void trocaSenhaAsync(){
		Usuario  usuarioTemp = usuarioLogin.getUsuario();
		usuarioTemp.setSenha(novaSenhaTBox.getValue());
		usuarioService.trocaSenha( usuarioTemp, new AsyncCallback<Boolean>() {

			public void onSuccess(Boolean arg0) {
				usuarioLogin.getUsuario().setSenha(novaSenhaTBox.getValue());
				if (getCallback()!=null){
					getCallback().onOk(null);
				}
			}

			public void onFailure(Throwable erro) {
				showErroAltera(erro.getMessage());
			}

		});
	}
	

	private Boolean checaCampoEmBrancoAltera(){
		showErroAltera("");
		
		if( senhaAtualTBox == null || "".equals(senhaAtualTBox.getValue())){
			showErroAltera("Informe o e-Mail atual");
			return false;
		}
		
		if( novaSenhaTBox == null || "".equals(novaSenhaTBox.getValue())){
			showErroAltera("Informe a Nova senha");
			return false;
		}
		
		if( confirmaSenhaTBox == null || "".equals(confirmaSenhaTBox.getValue())){
			showErroAltera("Confirme a Nova senha");
			return false;
		}
		
		if( !confirmaSenhaTBox.getValue().equals(novaSenhaTBox.getValue())){
			showErroAltera("A senha de confirmacao nao confere com a nova senha");
			return false;
		}

		return true;
	}
	
	private void showErroAltera(String msg) {
		msgErroAlteraLabel.setText(msg);
	}

	public EditAsyncCallback<AcessoCam> getCallback() {
		return callback;
	}

	public void setCallback(EditAsyncCallback<AcessoCam> callback) {
		this.callback = callback;
	}


}
